import { Message } from 'element-ui'
import store from '@/store/index'
import Vue from 'vue'
import VueRouter, { RouteConfig } from 'vue-router'
// 使用路由懒加载，避免js包过大，影响页面的加载，只有在路由被访问的时候才加载对应组件
// 资源按照对应的页面进行划分，需要的时候才加载对应的页面资源
// 没有指定webpackChunkName,打包后还是一个组件一个js，指定后可以把先关的资源合并打包放在一个js中，打包后的js文件名为[id].js,
// 1.登录界面
const Login = () => import(/* webpackChunkName: 'Login' */'../views/login.vue')
// 2.个人中心相关一个js
const personalCenter = () => import(/* webpackChunkName: 'personalCenter' */'../views/user/personalCenter.vue')
// 2.1 个人中心全部文章
const allArticles = () => import(/* webpackChunkName: 'personalCenter' */'../views/user/my-article/allArticles.vue')
// 2.2 个人中心草稿箱
const draftBox = () => import(/* webpackChunkName: 'personalCenter' */'../views/user/my-article/draftBox.vue')
// 2.3 待审核文章
const waitReview = () => import(/* webpackChunkName: 'personalCenter' */'../views/user/my-article/waitReview.vue')
// 2.4 被拒绝的文章
const rejectedArticles = () => import(/* webpackChunkName: 'personalCenter' */'../views/user/my-article/rejectedArticles.vue')
// 2.5 个人中心已发布的文章
const publishedArticles = () => import(/* webpackChunkName: 'personalCenter' */'../views/user/my-article/publishedArticles.vue')
// 2.6 个人中心我的收藏界面
const myCollection = () => import(/* webpackChunkName: 'personalCenter' */'../views/user/components/myCollection.vue')
// 2.7 个人中心的我的关注界面
const myFollow = () => import(/* webpackChunkName: 'personalCenter' */'../views/user/components/myFollow.vue')
// 2.8 个人中心我的评论界面
const myComment = () => import(/* webpackChunkName: 'personalCenter' */'../views/user/components/myComment.vue')
// 2.9 个人中心我的点赞页面
const myLike = () => import(/* webpackChunkName: 'personalCenter' */'../views/user/components/myLike.vue')
// 2.9 个人中心数据可视化界面
const charts = () => import(/* webpackChunkName: 'personalCenter' */'../views/user/components/charts.vue')
// 3.账号信息界面
// 3.1 父组件
const accountInformation = () => import(/* webpackChunkName: 'accountInformation */'../views/user/accountInformation.vue')
// 3.2 个人资料界面
const personalData = () => import(/* webpackChunkName: 'accountInformation' */'../views/user/components/personalData.vue')
// 3.3 账号设置页面
const accountSetting = () => import(/* webpackChunkName: 'accountInformation' */'../views/user/components/accountSetting.vue')
const changePassWord = () => import(/* webpackChunkName: 'accountInformation' */'../views/user/components/changePassWord.vue')
const accountCancel = () => import(/* webpackChunkName: 'accountInformation' */'../views/user/components/accountCancel.vue')
const changeEmail = () => import(/* webpackChunkName: 'accountInformation' */'../views/user/components/changeEmail.vue')
// 4 首页界面
const userMain = () => import(/* webpackChunkName: 'userMain' */'../views/user/userMain.vue')
// 5. 高级检索界面
const advancedSearch = () => import(/* webpackChunkName: 'advancedSearch' */'../views/user/advancedSearch.vue')
// 6. 普通检索的结果页面
const generalSearch = () => import(/* webpackChunkName: 'generalSearch' */'../views/user/generalSearch.vue')
// 7. 编写博客页面
const writeBlog = () => import(/* webpackChunkName: 'writeBlog' */'../views/writeBlog.vue')
// 8.1 文心一言的问答界面
const AIView = () => import(/* webpackChunkName: 'wenXinBot' */'../views/AI.vue')
// 8.审核项目整体界面
const audit = () => import(/* webpackChunkName: 'audit' */'../views/auditor/audit.vue')
//审核具体知识界面
const ExactAudit = () => import(/* webpackChunkName: 'audit' */'../views/auditor/ExactAudit.vue')

// 文章详情界面
const blogDetail = () => import(/* webpackChunkName: 'blogDetail' */'../views/blog/blogDetail.vue')

const FindPassword = () => import(/* webpackChunkName: 'FindPassword' */'../views/auditor/FindPassword.vue')
//管理员注册账号
const admin = () => import(/* webpackChunkName: 'EditTagDialog' */'../views/admin/admin.vue')


Vue.use(VueRouter)
const routes: Array<RouteConfig> = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/userMain',
    name: 'userMain',
    component: userMain
  },
  {
    path: '/generalSearch',
    name: 'generalSearch',
    component: generalSearch
  },
  {
    path: '/advancedSearch',
    name: 'advancedSearch',
    component: advancedSearch
  },
  {
    path: '/writeBlog',
    name: 'writeBlog',
    component: writeBlog
  },
  {
    path: '/blog-detail',
    name: 'blogDetail',
    component: blogDetail
  },
  {
    path: '/personalCenter',
    name: 'personalCenter',
    component: personalCenter,
    children: [
      { path: 'all-articles', name: 'allArticles', component: allArticles },
      { path: 'published-articles', name: 'publishedArticles', component: publishedArticles },
      { path: 'draft-box', name: 'draftBox', component: draftBox },
      { path: 'wait-review', name: 'waitReview', component: waitReview },
      { path: 'rejected-articles', name: 'rejectedArticles', component: rejectedArticles },
      { path: 'my-collection', name: 'myCollection', component: myCollection },
      { path: 'my-follow', name: 'myFollow', component: myFollow },
      { path: 'my-comment', name: 'myComment', component: myComment },
      { path: 'my-like', name: 'myLike', component: myLike },
      { path: 'charts', name: 'charts', component: charts }
    ]
  }
  ,
  {
    path: '/accountInformation',
    name: 'accountInformation',
    component: accountInformation,
    children: [
      {
        path: 'account-setting', name: 'accountSetting', component: accountSetting, children: [
          { path: 'change-password', name: 'changePassWord', component: changePassWord },
          { path: 'account-cancel', name: 'accountCancel', component: accountCancel },
          { path: 'change-email', name: 'changeEmail', component: changeEmail }
        ]
      },
      { path: 'personal-data', name: 'personalData', component: personalData },
    ]
  },
  {
    path: '/AIView',
    name: 'AIView',
    component: AIView,
  },
  {
    path: '/audit',
    name: 'audit',
    component: audit,
  },
  {
    path: '/FindPassword',
    name: 'FindPassword',
    component: FindPassword
  },
  {
    path: '/admin',
    name: 'admin',
    component: admin
  },
  {
    path: '/ExactAudit',
    name: 'ExactAudit',
    component: ExactAudit
  },
]

const router = new VueRouter({
  routes
})



router.beforeEach((to, from, next) => {
  // 如果访问的是登录页面，直接放行
  if (to.name === 'login') {
    console.log('router to login');
    // 清空本地内容
    localStorage.clear()
    next()
  } else if (to.name === 'FindPassword') {
    localStorage.clear()
    next()
  } else {
    const token = localStorage.getItem('token')
    if (token) {
      next()
    } else {
      Message.error('请先登录')
      next({ name: 'login' })
    }
    // 如果访问的不是登录页面，判断是否有token
  }
})


export default router